// pages/moviemore/moviemore.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movieList:[],
    count:12,
    start:0,
    url:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    wx.showNavigationBarLoading();
    util.getMovieList(options.url, "查看更多", { start: that.data.start,count: that.data.count }).then((movieList) => {
          that.setData({
            movieList: util.movieDataFactory(movieList),
            url:options.url,
            start: that.data.start + that.data.count
          });
          wx.setNavigationBarTitle({
            title: "查看更多"
          })
          wx.hideNavigationBarLoading();
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("用户下拉动作")
    if (this.data.url === '') {
      return;
    }
    var that = this;
    wx.showNavigationBarLoading();
    util.getMovieList(this.data.url, "查看更多", { start: 0, count: that.data.count }).then((movieList) => {
      that.setData({
        movieList: util.movieDataFactory(movieList),
        start: that.data.count
      });
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("到达底部咯")
    if(this.data.url === ''){
      return ;
    }
    var that = this;
    wx.showNavigationBarLoading();
    util.getMovieList(this.data.url, "查看更多", { start: that.data.start,count: this.data.count }).then((movieList) => {
      // 注意这里我们的数据结构是这样子的
      // var data = {
      //   url:'',
      //   title:'',
      //   subjects:[]
      // }
      // 所以我们要增加的是subjects数组，使用concat函数
      var newdata = util.movieDataFactory(movieList);
      var olddata = that.data.movieList;
      olddata.subjects = olddata.subjects.concat(newdata.subjects)
      that.setData({
        movieList: olddata ,
        start: that.data.start + that.data.count
      });
      wx.hideNavigationBarLoading();
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})